<?php

namespace app\adminmall\controller;
use app\adminmall\model\ComplaintModel;
use app\common\controller\AdminBaseController;
use think\Db;

/**
 * 投诉建议
 */
class ComplaintController extends AdminBaseController
{
 	 protected $model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new ComplaintModel();
    }
    /**
     * 广告轮播列表
     * @author [lx] 
     */
    public function index( $page = 1){
    	$map=[];
        $list = $this->model->paginate(10);
        if(request()->isPost()){
        	$param=input('post.');
        	if($param['name']){
        		$map['uname']=['like','%'.$param['name'].'%'];
        	}
        	if($param['status']){
        		$map['status']=$param['status'];
        	}
        	if($param['complaint_type']){
        		$map['complaint_type']=$param['complaint_type'];
        	}
        	if($param['start_time']){
        		$list = $this->model->whereTime('addtime', '>=', $param['start_time'])->where($map)->paginate(10);
        		return $this->fetch('index', ['list' => $list]);
        	}
        	
			$list = $this->model->where($map)->paginate(10); 

		}
      
        return $this->fetch('index', ['list' => $list]);
    }


/**
     * 回复
     * @author [lx] 
     */
    public function reply($id){
        $id = input('param.id');
        $list = $this->model->find($id);

        if($this->request->isAjax()){
        	$param=input('post.');
        	if($this->model->allowField(true)->save($param,$id) !== false){
        		$this->success("回复成功",url("Complaint/index"));
        	}else{
        	$this->error('回复失败');
        	}
        	
        }
        return $this->fetch('reply', ['ad' => $list]);
    }
    
    
    
    /**
     * 建议与投诉详情页
     * @author [lx] 
     */
    public function detail($id){
        $id = input('param.id');
        $list = $this->model->find($id);

       
        return $this->fetch('detail', ['ad' => $list]);
    }
    
    
    
    
    
    /**
     * 
     *添加
     * @author [lx] 
     */
    public function add()
    {
        if(request()->isAjax()){
        	$param=input('post.');
			$param['add_time']=now_time();
			$param['agent_id']=-1;
			if ($this->model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('Ads/index'));
            } else {
                $this->error('修改失败');
            }
        }
        return $this->fetch();
    }


    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [lx]
     */
    public function edit($id)
    {
        if(request()->isAjax()){
            $param = input('post.');         
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('Ads/index'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }



    /**
     * [del删除]
     * @author [lx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}